package com.xwj.back.service.impl.user;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xwj.back.mapper.UserMapper;
import com.xwj.back.pojo.User;
import com.xwj.back.service.user.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class RegisterServiceImpl implements RegisterService {

    private final static String  PhotoUrl = "http://localhost:3030/upload/f655f34c5e1c483999d9e559ffa4e14a.jpg";


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @Override
    public String Register(Map<String, String> data) {
        String username = data.get("username");
        String password = data.get("password");
        String confirmedPassword = data.get("confirmedPassword");
        String phone = data.get("phone");
        String role = data.get("role");
        if(!password.equals(confirmedPassword))
            return "PsError";
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        if(user != null){
            return "usernameError";
        }
        if(password.length() < 5){
            return "PsShort";
        }
        userMapper.insert(new User(null, username, passwordEncoder.encode(password),
                PhotoUrl, phone, role, true, null));
        return "success";
    }
}
